<template>
  <div class="app-container">
    <div class="schema-edit-container">
      <div class="base-info">
        <el-upload
          class="avatar-uploader"
          :action="uploadIconUrl"
          :show-file-list="false"
          :headers="uploadHeaders"
          :on-success="handleAvatarSuccess"
        >
          <el-image v-if="iconSrc" :src="iconSrc" fit="cover" />
          <i v-else class="el-icon-plus avatar-uploader-icon" />
        </el-upload>
        <div><span class="el-form-item__label">名称:</span><span style="line-height: 40px;">{{ schemaName }}</span></div>
        <div style="margin-left: 10px"><router-link :to="{name: 'cmdb_instances', query: {schemadef: schemaId}}" class="el-button el-button--text el-button">所有实例 >>>:</router-link></div>
      </div>

      <el-tabs v-model="activeName">
        <el-tab-pane label="拓扑" name="first">
          <topo v-if="activeName === 'first'" :schemadef="schemaId" />
        </el-tab-pane>
        <el-tab-pane label="属性" name="second" style="width: 80%;">
          <attr-form v-if="activeName === 'second'" :schemadef="schemaId" />
        </el-tab-pane>
        <el-tab-pane label="关系" name="third">
          <relation v-if="activeName === 'third'" :schemadef="schemaId" />
        </el-tab-pane>
        <el-tab-pane label="标签" name="forth">
          <tag v-if="activeName === 'forth'" :schemadef="schemaId" />
        </el-tab-pane>
      </el-tabs>
    </div>
  </div>
</template>

<script>
import { detail, getIcon, uploadIcon } from '@neu/cmdb-ui/api/schema'
import AttrForm from './attrForm'
import Tag from './tag'
import Relation from './relation'
import Topo from './topo'
import { getToken } from '@neu/biz-core/utils/auth'

export default {
  name: 'CmdbSchemaDetailPage',
  components: {
    AttrForm,
    Tag,
    Relation,
    Topo
  },
  data() {
    return {
      requestLoading: false,
      schemaId: '',
      schemaName: '',
      activeName: 'first',
      iconSrc: '',
      uploadIconUrl: uploadIcon(this.$route.params.id)
    }
  },
  computed: {
    uploadHeaders: function() {
      return {
        'Authentication': getToken()
      }
    }
  },
  created() {
    this.schemaId = this.$route.params.id
    this.$store.dispatch('cmdb/loadDatatypes')
    this.$store.dispatch('cmdb/loadRelationtypes')

    // 表单组件放在diaolog中，首次打开时，监控不到id的变化。所以需要在created中调用一次
    if (this.schemaId !== '') {
      this.fetch()
      getIcon(this.schemaId).then(response => {
        if (response.data && response.data.url) {
          this.iconSrc = response.data.url
        }
      })
    }
  },
  methods: {
    // 初始化表单数据
    fetch() {
      if (this.schemaId !== '') {
        this.requestLoading = true
        detail(this.schemaId).then(response => {
          const { name } = response.data
          this.schemaName = name
          this.requestLoading = false
        })
      }
    },
    handleAvatarSuccess(response) {
      this.iconSrc = response.data.url
    }
  }
}
</script>

<style lang="scss">

.schema-edit-container {
   background-color: #ffffff;
   padding: 3px 20px;

  .base-info {
    display: flex;
    align-items: center;
    padding: 3px 0;
    background-color: #ffffff;
    .el-image {
      width: 48px;
      height: 48px;
      border-radius: 50%;
    }
  }

  .avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
    margin-right: 20px;
  }
  .avatar-uploader .el-upload:hover {
    border-color: #409EFF;
  }
  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 48px;
    height: 48px;
    line-height: 48px;
    text-align: center;
  }
  .avatar {
    width: 178px;
    height: 178px;
    display: block;
  }

  .validate-error {
    input {
      border-color: #ff4949;
    }
  }
}
</style>
